package appapi.service.impl;

import appapi.Request.Basic.EquipMaintenanItemReq;
import appapi.Request.Basic.EquipSelfchecjItemReq;
import appapi.Vo.AddEquipMainItemVo;
import appapi.Vo.EditEquipSelfItemVo;
import appapi.Vo.device.EquipMainItemVo;
import appapi.entity.basic.EquipselfcheckitemEntity;
import appapi.service.EquipmaintenanitemService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import appapi.dao.EquipmaintenanitemMapper;
import appapi.entity.basic.EquipmaintenanitemEntity;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 请求对象
 *
 * @author luocheng
 * @version 1.0
 * @date 2023/05/27 13:39
 */
@Service
public class EquipmaintenanitemServiceImpl implements EquipmaintenanitemService {

    @Resource
    private EquipmaintenanitemMapper equipmaintenanitemMapper;

    @Override
    public List<EquipmaintenanitemEntity> getList(EquipMaintenanItemReq req) {
        return equipmaintenanitemMapper.queryList(req);
    }

    @Override
    public EquipmaintenanitemEntity detail(Long mainItemId) {
        return equipmaintenanitemMapper.selectByPrimaryKey(mainItemId);
    }

    @Override
    public int count(EquipmaintenanitemEntity record) {
        return equipmaintenanitemMapper.count(record);
    }

    @Override
    public int deleteByPrimaryKey(Long mainItemId) {
        return equipmaintenanitemMapper.deleteByPrimaryKey(mainItemId);
    }

    @Override
    public int insert(List<EquipmaintenanitemEntity> list) {
        return equipmaintenanitemMapper.insertList(list);
    }

    @Override
    public int updateByPrimaryKey(EquipmaintenanitemEntity record) {
        return equipmaintenanitemMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateBatch(AddEquipMainItemVo record){
        EquipMaintenanItemReq req =new EquipMaintenanItemReq();
        req.setEquipId(record.getEquipId());
        //获取设备原有维保项目
        List<EquipmaintenanitemEntity> oldList =equipmaintenanitemMapper.queryList(req);
        List<EquipmaintenanitemEntity> addList =new ArrayList<>();
        for (EquipMainItemVo vo:record.getItemVoList()
        ) {
            //新增
            if(oldList.stream().filter(EquipmaintenanitemEntity -> EquipmaintenanitemEntity.getMainItemId().equals(vo.getMainItemId())).count()==0) {
                EquipmaintenanitemEntity entity = new EquipmaintenanitemEntity();
                entity.setEquipId(vo.getEquipId());
                entity.setItemName(vo.getItemName());
                entity.setItemDesc(vo.getItemDesc());
                entity.setCreateTime(LocalDateTime.now());
                entity.setUpdateTime(LocalDateTime.now());
                addList.add(entity);
            }
        }
        //删除原有类型
        List<Long> delList =new ArrayList<>();
        List<String> ids =record.getItemVoList().stream().map(EquipMainItemVo->EquipMainItemVo.getItemName()).collect(Collectors.toList());
        for (EquipmaintenanitemEntity old:oldList
        ) {
            if(ids.stream().filter(itemName -> itemName.equals(old.getItemName())).count()==0) {
                delList.add(old.getMainItemId());
            }
        }
        if(delList.size()>0) {
            equipmaintenanitemMapper.deleteBatch(delList);
        }
        if(addList.size()>0) {
            equipmaintenanitemMapper.insertBatch(addList);
        }
        return 1;
    }

}
